package com.cz.nongyetongv5.task;

import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.utils.UpdateDataUtil;

public class SynchronizedDataService extends IntentService {

	public SynchronizedDataService() {
		super(SynchronizedDataService.class.getName());
	}

	// private UpdateDataUtil updateDataUtil = null;

	private DatabaseHelper db = null;

	private static final String TAG = "synservice";

	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

	@Override
	public void onCreate() {

		db = new DatabaseHelper(this, CommonConstants.DBNAME, null,
				CommonConstants.DATABASE_VERSION);

		super.onCreate();
	}

	@Override
	public void onDestroy() {

		// 清理资源
		db.close();

		super.onDestroy();
	}
	@Override
	protected void onHandleIntent(Intent intent) {
		Log.d(TAG, "移除同步... ");
	}

	protected void _onHandleIntent(Intent intent) {

		Log.d(TAG, "同步启动成功... 开始同步... ");

		User user = (User) intent.getSerializableExtra("user");

		UpdateDataUtil dataUtil = new UpdateDataUtil(db,
				SynchronizedDataService.this, user);

		//		dataUtil.add("TOWN");
		//
		//		Log.d(TAG, "Town同步完成...");

		dataUtil.add("BEIJIANDANWEI");

		dataUtil.update("BEIJIANDANWEI");

		dataUtil.delete("BEIJIANDANWEI");

		Log.d(TAG, "被检单位同步完成...");

		dataUtil.add("JIANCHADANWEI");
		dataUtil.update("JIANCHADANWEI");
		dataUtil.delete("JIANCHADANWEI");
		Log.d(TAG, "检测单位同步完成...");

		dataUtil.add("JIANGUANRENYUAN");
		dataUtil.update("JIANGUANRENYUAN");
		dataUtil.delete("JIANGUANRENYUAN");
		Log.d(TAG, "监管人员同步完成...");

		dataUtil.add("LINKMAN");
		dataUtil.update("LINKMAN");
		dataUtil.delete("LINKMAN");
		Log.d(TAG, "联系人同步完成...");

		dataUtil.add("CHOUYANJIGOU");
		dataUtil.update("CHOUYANJIGOU");
		dataUtil.delete("CHOUYANJIGOU");
		Log.d(TAG, "抽验机构同步完成...");

		Log.d(TAG, "同步完成!!");

	}
}
